// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: chensili

#include <gflags/gflags.h>

#include "cc/coin2/strategy/strategy.h"
#include "coin2/app/driver.h"

DEFINE_string(driver, "", "driver config");
DEFINE_int32(exit_after_sec, 3600, "");

namespace appcoin2::support::order {

class AccountInfoCollector : public ::coin2::strategy::IStrategy {
 public:
  AccountInfoCollector(std::optional<int64_t> timestamp,
                       int exit_after_sec)
    : strategy_created_time_(timestamp.has_value() ? *timestamp : GetCurrentTimestamp()),
      exit_after_ns_(exit_after_sec * 1e9) {}
  void onAccountInfo(const coin2::exchange::base::order::OrderUpdate& upd) {
    if (GetCurrentTimestamp() - strategy_created_time_ > exit_after_ns_) {
      std::exit(0);
    }
  }
 private:
  int64_t strategy_created_time_;
  int64_t exit_after_ns_;
};


}  // namespace appcoin2::support::order

int main(int argc, char* argv[]) {
  google::InitGoogleLogging(argv[0]);
  gflags::ParseCommandLineFlags(&argc, &argv, true);

  auto path = std::experimental::filesystem::path(FLAGS_driver);
  coin2::app::DriverConfig app_config;
  app_config.MergeFrom(coin2::base::config::DriverConfigFromJson(path));
  LOG(INFO) << "app_config: " << app_config.DebugString();

  appcoin2::support::order::AccountInfoCollector strategy(
      std::optional<int64_t>(), FLAGS_exit_after_sec);
  coin2::app::Driver driver(app_config, &strategy);
  driver.RegisterCleanupHandler();
  driver.Run();
  return 0;
}
